exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
    constructor() {
        super();
        const db = this.db;
        this.orgModel = db['sys_org'];
        this.userModel = db['sys_user'];
        this.serverIP = 'http://' + think.config('staticIp') + ':' + think.config('proxyPort');
    }
    getOrgList(userInfo) {
        var _this = this;

        return _asyncToGenerator(function* () {
            try {
                return yield _this.BaseService.dataScopeFilter('sys_org', userInfo);
            } catch (err) {
                return _this.exceptionService.handleError(err);
            }
        })();
    }
    getUserListByOrg(orgData) {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            try {
                let userlist = yield _this2.userModel.findAll({ where: { org_id: orgData.org_id } });
                if (userlist.length > 0) {
                    userlist = userlist.map(function (item) {

                        if (item.pic_path) {
                            item.pic_path = _this2.serverIP + '/' + item.pic_path;
                        } else {
                            item.pic_path = _this2.serverIP + '/static/upload/images/default.png';
                        }
                        return item;
                    });
                }
                return userlist;
            } catch (err) {
                return _this2.exceptionService.handleError(err);
            }
        })();
    }
    getOrgs(data) {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            let orgList = [];
            let userList = [];
            const Op = _this3.db.Sequelize.Op;
            try {
                orgList = yield _this3.orgModel.findAll({
                    where: {
                        parent_id: data.id
                    }
                });
                // orgList = await this.getChildeFlag(orgList);
                userList = yield _this3.userModel.findAll({
                    where: {
                        org_id: data.id
                    },
                    attributes: { exclude: ['last_login_time', 'is_login', 'login_ip', 'login_flag', 'del_flag', 'remarks', 'create_by', 'update_by', 'create_date', 'update_date'] }
                });
                if (userList.length > 0) {
                    userList = userList.map(function (item) {
                        if (item.pic_path) {
                            item.pic_path = _this3.serverIP + '/' + item.pic_path;
                        } else {
                            item.pic_path = _this3.serverIP + '/static/upload/images/default.png';
                        }
                        return item;
                    });
                }
                const result = { userList: userList, orgList: orgList };
                return result;
            } catch (err) {
                return _this3.exceptionService.handleError(err);
            }
        })();
    }
    /**
     * 查询当前机构是否存在子集或用户
     * list --机构列表
     */
    getChildeFlag(list) {
        var _this4 = this;

        return _asyncToGenerator(function* () {
            try {
                for (var i = 0; i < list.length; i++) {
                    let data = list[i].dataValues;
                    const orgCount = yield _this4.orgModel.count({ where: { parent_id: data.id } });
                    const userCount = yield _this4.userModel.count({ where: { org_id: data.id } });
                    if (orgCount > 0 || userCount > 0) {
                        data.child = true;
                    } else {
                        data.child = false;
                    }
                }
                return list;
            } catch (err) {
                return _this4.exceptionService.handleError(err);
            }
        })();
    }
    /**
     * 递归查询所有子孙机构
     * @param {*} data 
     */
    getOrgIds(data) {
        var _this5 = this;

        return _asyncToGenerator(function* () {
            let idArr = [data.id];
            const Op = _this5.db.Sequelize.Op;
            try {
                const getList = (() => {
                    var _ref = _asyncToGenerator(function* (childArr) {
                        const where = {
                            parent_id: {
                                [Op.in]: childArr
                            }
                        };
                        const childids = yield _this5.orgModel.findAll({
                            where: where,
                            attributes: ['id']
                        });
                        if (childids && childids.length > 0) {
                            let childid = [];
                            for (let i = 0; i < childids.length; i++) {
                                idArr.push(childids[i].id);
                                childid.push(childids[i].id);
                            }
                            return yield getList(childid);
                        } else {
                            return idArr;
                        }
                    });

                    return function getList(_x) {
                        return _ref.apply(this, arguments);
                    };
                })();
                return yield getList(idArr);
            } catch (err) {
                return _this5.exceptionService.handleError(err);
            }
        })();
    }

    /**
     * 
     */
    // async getOrgs(data){
    //     let idArr = [data.id];
    //     let list = [];
    //     const Op = this.db.Sequelize.Op;
    //     try{
    //         const getList = async (childArr)=> {
    //             const where = {
    //                 parent_id: {
    //                     [Op.in]: childArr
    //                 }
    //             };
    //             const childids = await this.orgModel.findAll({
    //                 where: where,
    //                 attributes: ['id']
    //             });
    //             if (childids && childids.length > 0) {
    //                 let childid = [];
    //                 for (let i = 0; i < childids.length; i++) {
    //                     idArr.push(childids[i].id);
    //                     childid.push(childids[i].id);
    //                 }
    //                 return await getList(childid);
    //             } else {
    //                 return idArr;
    //             }

    //         };
    //         const ids = await getList(idArr);
    //         if(ids&&ids.length>0){
    //             list = await this.orgModel.findAll({
    //                 where:{
    //                     id:{
    //                         [Op.in]:ids
    //                     }
    //                 }
    //             })
    //         }
    //         return list;
    //     } catch (err) {
    //         return this.exceptionService.handleError(err);
    //     }
    // }

    getAllOrg() {
        var _this6 = this;

        return _asyncToGenerator(function* () {
            try {
                return yield _this6.orgModel.findAll({
                    attributes: ['id', 'parent_id', 'org_name', 'org_code']
                });
            } catch (err) {
                return _this6.exceptionService.handleError(err);
            }
        })();
    }
};